package cn.smile.service;

import cn.smile.entity.Order;
import cn.smile.utils.ByteArrayUtils;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Optional;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: MaoSuyu
 * @User：John
 * @Date: 2019/6/4
 * @Time: 16:25
 * @Description: No Description
 */
@Component
public class RabbitConsumer {

    //参数1：被拒绝的消息的索引，参数2：是否将拒绝的消息重新放回队列
    // channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
    //参数1：接收成功的消息索引，参数2：是否一次性拒绝小于该消息索引的全部队列
    // channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    //参数1：被拒绝的消息的索引，参数2：是否一次性拒绝小于该消息索引的全部队列，是否将拒绝的消息重新放回队列
    // channel.basicNack(message.getMessageProperties().getDeliveryTag(),true,false);

    @RabbitListener(queues={"q2"})
    public void consumer1(Message message, Channel channel) throws IOException {
        final Optional<Object> order = ByteArrayUtils.bytesToObject(message.getBody());
        Order o= (Order) order.get();
        System.out.println("我是队列1："+o);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    @RabbitListener(queues={"q1"})
    public void consumer2(Message message, Channel channel) throws IOException {
        final Optional<Object> order = ByteArrayUtils.bytesToObject(message.getBody());
        Order o= (Order) order.get();
        System.out.println("我是队列2："+o);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

}
